/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.tabs;

import com.noocodecommit.wicket.stuff.jquery.core.JQAjaxOptions;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQTabsUtils
{

	/**
	 * Create a JQTabsBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQTabsBuilder
	 */
	public static JQTabsBuilder create(String id, String key)
	{
		return new JQTabsBuilder(new JQTabs(id, (key == null ? JQTabsPlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQTabsBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQTabsBuilder
	 */
	public static JQTabsBuilder create(String id)
	{
		return new JQTabsBuilder(new JQTabs(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQTabsBuilder
	{
		/** JQuery component */
		private JQTabs component;
		
		/** JQuery default options */
		private JQTabsOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQTabsBuilder(JQTabs component)
		{
			this.component = component;
			this.options = new JQTabsOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the seleted option value
		 * 
		 * @param selected
		 *            tab selected
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder selected(String selected)
		{
			options.setSelected(selected);
			return this;
		}

		/**
		 * Sets the unselect option value
		 * 
		 * @param unselect
		 *            tab unselect
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder unselect(boolean unselect)
		{
			options.setUnselect(unselect);
			return this;
		}

		/**
		 * Sets the event option value
		 * 
		 * @param event
		 *            tab event
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder event(String event)
		{
			options.setEvent(event);
			return this;
		}

		/**
		 * Sets the disabled option value
		 * 
		 * @param disabled
		 *            tab disabled
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder disabled(int[] disabled)
		{
			options.setDisabled(disabled);
			return this;
		}

		/**
		 * Sets the spinner option value
		 * 
		 * @param spinner
		 *            tab spinner
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder spinner(String spinner)
		{
			options.setSpinner(spinner);
			return this;
		}

		/**
		 * Sets the cache option value
		 * 
		 * @param cache
		 *            tab cache
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder cache(boolean cache)
		{
			options.setCache(cache);
			return this;
		}

		/**
		 * Sets the ajaxOptions option value
		 * 
		 * @param ajaxOptions
		 *            tab ajaxOptions
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder ajaxOptions(JQAjaxOptions ajaxOptions)
		{
			options.setAjaxOptions(ajaxOptions);
			return this;
		}

		/**
		 * Sets the idPrefix option value
		 * 
		 * @param idPrefix
		 *            tab idPrefix
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder idPrefix(String idPrefix)
		{
			options.setIdPrefix(idPrefix);
			return this;
		}

		/**
		 * Sets the fx option value
		 * 
		 * @param fx
		 *            tab fx
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder fx(String fx)
		{
			options.setFx(fx);
			return this;
		}

		/**
		 * Sets the tabTemplate option value
		 * 
		 * @param tabTemplate
		 *            tab tabTemplate
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder tabTemplate(String tabTemplate)
		{
			options.setTabTemplate(tabTemplate);
			return this;
		}

		/**
		 * Sets the panelTemplate option value
		 * 
		 * @param panelTemplate
		 *            tab panelTemplate
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder panelTemplate(String panelTemplate)
		{
			options.setPanelTemplate(panelTemplate);
			return this;
		}

		/**
		 * Sets the select function
		 * 
		 * @param selectScript
		 *            tab select function
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder selectScript(String selectScript)
		{
			options.setSelectScript(selectScript);
			return this;
		}

		/**
		 * Sets the load function
		 * 
		 * @param loadScript
		 *            tab load function
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder loadScript(String loadScript)
		{
			options.setLoadScript(loadScript);
			return this;
		}

		/**
		 * Sets the show function
		 * 
		 * @param showScript
		 *            tab show function
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder showScript(String showScript)
		{
			options.setShowScript(showScript);
			return this;
		}

		/**
		 * Sets the add function
		 * 
		 * @param addScript
		 *            tab add function
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder addScript(String addScript)
		{
			options.setAddScript(addScript);
			return this;
		}

		/**
		 * Sets the remove function
		 * 
		 * @param removeScript
		 *            tab remove function
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder removeScript(String removeScript)
		{
			options.setRemoveScript(removeScript);
			return this;
		}

		/**
		 * Sets the enable function
		 * 
		 * @param enableScript
		 *            tab enable function
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder enableScript(String enableScript)
		{
			options.setEnableScript(enableScript);
			return this;
		}

		/**
		 * Sets the disable function
		 * 
		 * @param disableScript
		 *            tab disable function
		 * @return JQTabsBuilder
		 */
		public JQTabsBuilder disableScript(String disableScript)
		{
			options.setDisableScript(disableScript);
			return this;
		}

		/**
		 * @return JQTabs
		 */
		public JQTabs build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}